﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Gateway.Interfaces;
using Phoenix.ZeroMQConnectors;

namespace PServiceBus.Gateway.ZeroMQ {

    public class ZeroMQGatewayServer : IGatewayServer {

        private ZeroMQReceiver _server;

        public ZeroMQGatewayServer() { }

        public ZeroMQGatewayServer(string address) {
            _server = new ZeroMQReceiver(ZeroMQEndpointUri.Parse(address));
        }

        #region IGatewayServer Members

        public Action<Exception> ErrorReceived { get; set; }

        public void Register<TCommand>(Action<TCommand> command) where TCommand : class {
            _server.Register<TCommand>(command);
        }

        public void Register<TRequest, TResponse>(Func<TRequest, TResponse> command)
            where TRequest : class
            where TResponse : class {
                _server.Register<TRequest, TResponse>(command);
        }

        public void Start() {
            _server.Start();
        }

        public IGatewayServer Init(string address) {
            return new ZeroMQGatewayServer(address);
        }

        #endregion

        #region IDisposable Members

        public void Dispose() {
            if (_server != null)
                _server.Dispose();
        }

        #endregion
    }
}
